﻿namespace RuoVea.Fams.Web.Controllers;

[Authorize(Roles = "None")]
public partial class LocationsController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly LocationsService _locationsService;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="locationsService"></param> 
    public LocationsController(LocationsService locationsService)
    {
        _locationsService = locationsService;
    }

    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("locations/pages")]
    public async Task<RestfulResult<PageResult<FamsLocations>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        LocationsPageParam? data = JsonConvert.DeserializeObject<LocationsPageParam>(dataStr);
        data ??= new LocationsPageParam { PageNo = 1, PageSize = 10 };

        var query = await _locationsService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 查询所有
    /// </summary>
    /// <returns></returns>
    [HttpGet("locations/list")]
    public async Task<RestfulResult<List<FamsLocations>>> GetlistAsync()
    {
        var restful = await _locationsService.GetlistAsync();
        return restful;
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("locations/add")]
    public async Task<RestfulResult> AddAsync([FromBody] FamsLocations data)
    {
        var restful = await _locationsService.AddAsync(data);
        return restful;
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("locations/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] FamsLocations data)
    {
        var restful = await _locationsService.UpdateAsync(data);
        return restful;
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("locations/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        var restful = await _locationsService.DeleteAsync(data);
        return restful;
    }
}